package agile.hustler.activities;

import agile.hustler.Characters.Caster;
import agile.hustler.Characters.Dps;
import agile.hustler.Characters.Tank;
import agile.hustler.activities.R;
import agile.hustler.activities.R.drawable;
import agile.hustler.activities.R.id;
import agile.hustler.activities.R.layout;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class NameScreen extends Activity {
    /** Called when the activity is first created. */
	TextView txtview;
	
	 String name;
	 EditText nameInput;
	 String name_string;
	 int typ = 0;
	 File file;
	 Drawable charIcon;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.namescreen);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        //txtview = (TextView)findViewById(R.id.TextView01);
        
        Bundle b = getIntent().getExtras();
        final int data = b.getInt("class");
        
        //txtview.setText(data.toString());
        
        nameInput = (EditText)findViewById(R.id.EditName);
        if(data == 1)
		  {
			  charIcon = getResources().getDrawable(R.drawable.tanksmalll);
		  }
		else if(data == 2)
		  {
			  charIcon = getResources().getDrawable(R.drawable.castersmalll);
		  }  
		else
		  {
			  charIcon = getResources().getDrawable(R.drawable.dpssmalll);
		  }
        ImageView imView = (ImageView)findViewById(R.id.CharacterImage);
  		imView.setBackgroundDrawable(charIcon);       
        findViewById(R.id.ButtonDone).setOnClickListener(new View.OnClickListener() {
        	
        	public void onClick(View v) {     		
        		if(nameInput.getText().length() <= 0)
        		{
        			Context context = getApplicationContext();
        			Toast toast = Toast.makeText(context, "Write your name, please", 2100);
        			toast.show();		
        	        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
        	        findViewById(R.id.EditName).startAnimation(shake);
        		}
        		
        		else if(nameInput.getText().length() > 7)
        		{
        			Context context = getApplicationContext();
        			Toast toast = Toast.makeText(context, "Max 7 letters", 2100);
        			toast.show(); 			
        	        Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
        	        findViewById(R.id.EditName).startAnimation(shake);
        		}    		
        	else
        		{   		
	            name = nameInput.getText().toString().replaceAll(" ", ".");
	      		file = new File(name + ".txt"); //"sdtext.txt"		  
	      		name_string = "test";
	      		if(data == 1)
	      		  {
	      			  Tank tank = new Tank(name, 1); 
	      			  name_string = tank.toString();
	      			  typ = 1;
	      		  }
	
	      		else if(data == 2)
	    		  {
	    			  Caster caster = new Caster(name, 1);  
	    			  name_string = caster.toString();
	    			  typ = 2;
	    		  }	  
	      		else
	      		  {
	      			  Dps dps = new Dps(name, 1);
	      			  name_string =  dps.toString();
	      			  typ = 3;
	      		  }
	      		  writeToFile(name_string);
	      		  Bundle B = new Bundle();
	      		  B.putString("key", name_string);
	      		  Intent intent = new Intent(NameScreen.this, CharacterScreen.class);
	      		  intent.putExtras(B);  
	      		  startActivity(intent);
	      		  finish();
	      		System.gc();
        		}
        	}
        });
        
        findViewById(R.id.ButtonBack).setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
				startActivity(new Intent(NameScreen.this, NewGame.class));
				finish();
				System.gc();
        	}
        });
    }
    
    /*
     * Skriver till en txt-fil
     * 
     */
    public void writeToFile(String input)
    {
    	
  	  //Writing to a file
	       try {

	    	   File datadir = new File("/sdcard/hustlerdata/");
	    	   File root = Environment.getExternalStorageDirectory();
	    	   File txtfile = new File(datadir, name + ".txt");  		   
	    	   FileWriter txtwriter = new FileWriter(txtfile);
	    	   BufferedWriter out = new BufferedWriter(txtwriter);
	    	   out.write(input);
	    	   out.close();
				    
		        } catch (IOException ioe) {
		            ioe.printStackTrace();
		       }		
    	
    }
    
}